package com.example.netactivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyTask implements Runnable {
    private Handler handler;

    public MyTask(Handler handler){ this.handler = handler;}

    private static final String TAG = "Net";
    @Override
    public void run () {
        Log.i(TAG, "run:线程已运行");
        //可以访问网络资源
        //延时，模拟操作耗时
        String retStr = "";
        Bundle retbundle = new Bundle();
        ArrayList<String> retlist = new ArrayList<>();
        try {
            Log.i(TAG, "run:正在工作。。。");
            Document doc = Jsoup.connect("https://chl.cn/?jinri").get();
            Log.i(TAG, "title=" + doc.title());
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.first();
            //Log.i(TAG, "run:table=" + table1);
            //查找tr
            Elements rows = table1.getElementsByTag("tr");
            rows.remove(0);

            for (Element row : rows) {
                // Log.i(TAG, "run:row=" + row);
                //拆分单元格td
                Elements tds = row.getElementsByTag("td");
                Element td1 = tds.first();
                Element td2 = tds.get(4);//第五列表格，即第一列是0开始
                String str1 = td1.text().trim();
                String str2 = td2.text().trim();
                Log.i(TAG, "run:币种:" + td1.text() + "价格：" + td2.text());
                Log.i(TAG, "run:币种2:" + td1.html() + "价格:" + td2.html());
                //结果->retStr
                //retStr +=("币种：" + td1.text() +"=>"+ "价格：" + td2.text() + "\n");
                if(str1.contains("美元")){
                    retbundle.putFloat("dollar",100/Float.parseFloat(str2));
                }else if(str1.contains("欧元")){
                    retbundle.putFloat("euro",100/Float.parseFloat(str2));
                }else if(str1.contains("韩国元")){
                    retbundle.putFloat("won",100/Float.parseFloat(str2));
                }
                retlist.add(str1 + "==>" + str2);
                retStr +=("币种：" + td1.text() +"=>"+ "价格：" + td2.text() + "\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);                                                   //!!!!
        } catch (IOException e) {
            e.printStackTrace();                                                              //!!!
            throw new RuntimeException(e);
        }
        //发送数据给主线程
        retbundle.putString("html",retStr);
        retbundle.putStringArrayList("mylist",retlist);
        Message msg = handler.obtainMessage();
        msg.what=7;
        msg.obj = retbundle;
        handler.sendMessage(msg);
    }
}
